<template>
  <el-card shadow="never" class="aui-card--fill">
    <el-form :inline="true" v-model="SearchForm" clearable  @keydown.enter.native.prevent="getDataList()">
      <el-form-item>
        <el-input v-model="SearchForm.susername" placeholder="提交学生姓名" clearable></el-input>
      </el-form-item>
        <el-form-item>
              <el-select v-model="SearchForm.status" placeholder="审核状态">
                  <el-option label="未审核" value=0></el-option>
                  <el-option label="审核通过" value=2></el-option>
                  <el-option label="已驳回" value=1></el-option>
                  <el-option label="所有开题报告" value=3></el-option>
              </el-select>
          </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
        </el-form-item>
      <el-form-item>
        <el-button  type="success" @click="approve()" :disabled="dataListSelections.length <= 0">批量通过</el-button>
        <el-button  type="primary" @click="reject()"  :disabled="dataListSelections.length <= 0">批量驳回</el-button>
        <el-button  type="danger"  @click="del()"  :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <!-- <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column> -->
      <el-table-column prop="createby" header-align="center" align="center" label="提交学生">
      </el-table-column>
      <!-- <el-table-column prop="susername" header-align="center" align="center" width="120" label="提交学生学号">
      </el-table-column> -->
      <!-- <el-table-column prop="prospect" header-align="center" align="center" width="120" :show-overflow-tooltip="true" label="选题意义或前景">
      </el-table-column>
      <el-table-column prop="route" header-align="center" align="center" width="120" :show-overflow-tooltip="true" label="研究方法路线">
      </el-table-column>
      <el-table-column prop="plan" header-align="center" align="center" width="120" :show-overflow-tooltip="true" label="总体安排计划"> 
      </el-table-column>-->
      <el-table-column header-align="center" align="center" width="200" prop="filename" show-overflow-tooltip
        label="开题报告">
        <template slot-scope="scope">
          <!-- <el-button type="text" @click="childHandle(scope.row)">{{ scope.row.filename }}</el-button> -->
          <file-down :pre-view="true" api-url="/teacher/down/report" :file-id="scope.row.id" :FileInfo="scope.row" size="small"
            icon="" type="text" :title="scope.row.filename"></file-down>
        </template>
      </el-table-column>
      <el-table-column prop="advise" header-align="center" align="center" :show-overflow-tooltip="true" label="审核意见">
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="审核情况">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="info">未审核</el-tag>
          <el-tag v-if="scope.row.status === 1" size="small" type="danger">已驳回</el-tag>
          <el-tag v-if="scope.row.status === 2" size="small" type="success">已通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="260" label="操作">
        <template slot-scope="scope">
          <file-down api-url="/teacher/down/report" :file-id="scope.row.id" :FileInfo="scope.row" size="small"
            icon="" type="text" title="下载"></file-down>
          <el-button type="text" size="small" @click="approve(scope.row.id)">批准通过</el-button>
          <el-button  type="text" size="small" @click="reject(scope.row.id)">驳回修改</el-button>
          <!-- <el-button  type="text" size="small" @click="detailed(scope.row.id)">查看详情</el-button> -->
          <el-button size="small"  type="text" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </el-card>
</template>

<script>
import servicesApi from '@/services/servicesApi';
import debounce from 'lodash/debounce';
import AddOrUpdate from '@/views/Teacher/check-dereport'
import FileDown from '@/components/FileDown.vue';
export default {
  data() {
    return {
      SearchForm: {
        susername: '',
        status: null,
      },
      dataList: [
        { 
         id: '',
         susername: '',
         prospect: '',
         route: '',
         plan: '',
         advise: '', 
         createby: '', 
         repTeacher: '', 
         repTeacher: '', 
         gmtCreate: '', 
         gmtModified: '', 
         status: null
         }
      ],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      deleteList: [],
      deleteList: {
              list: []
           },
      addOrUpdateVisible: false, // 新增／更新，弹窗visible状态
    }
  },
  activated() {
    this.getDataList()
  },
  created() {
    this.getDataList()
  },
  components: {
    AddOrUpdate,
    FileDown
},
  methods: {
      //删除
      del(id) {
      let roleIds = id ? [id] : this.dataListSelections.map(item => {
      return item.id
    })
    this.deleteList.list = roleIds
      servicesApi.delTreport(this.deleteList).then(res => {
          // console.log(res.data)
          let code = res.data.code
          if (code === 200) {
              this.$message({
                  message: '删除成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                      this.getDataList()
                  }
              })
          } else {
              this.$message({
                  message: res.data.msg,
                  type: 'danger',
                  duration: 1000,
                  onClose: () => {
                      this.getDataList()
                  }
              })
          }
      })
  },
  //查看详细信息
      detailed(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        // this.$refs.addOrUpdate.dataForm.id = id
        this.$refs['addOrUpdate'].init(id)
      })
    },
    // 获取数据列表
    getDataList: debounce(function() {
      this.dataListLoading = true
      let pageInfo = {
        current: this.pageIndex,
        size: this.pageSize,
        name: this.SearchForm.susername,
        status: this.SearchForm.status === null?3:this.SearchForm.status,
      }
      servicesApi.getTReport(pageInfo).then(res => {
        // console.log(res.data)
        let code =res.data.code
        let data = res.data.data
        if (code===200) {
          this.dataList = data.list
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
      // console.log('多选')
      // console.log(this.dataListSelections)
    },
    //批准通过
    approve(id) {
      let userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      // console.log(userIds)
      this.$prompt('请输入通过意见', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
      .then(({value}) => {
        // console.log(value)
        this.deleteList = userIds
        let checkList = {
        advise: value,
        list: this.deleteList,
        status: 2
      }
        servicesApi.checkTreport(checkList).then(res => {
          // console.log(res.data)
          let code = res.data.code
          if (code === 200) {
            this.$message({
              type: 'success',
              message: '批准通过成功',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {
            this.$message({
            type: 'warning',
            message: '取消批准通过'
          });  
       })
  },
  //驳回
    reject(id) {
      let userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      // console.log(userIds)
      this.$prompt('请输入驳回意见', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
      .then(({value}) => {
        // console.log(value)
        this.deleteList = userIds
        let checkList = {
        advise: value,
        list: this.deleteList,
        status: 1
      }
        servicesApi.checkTreport(checkList).then(res => {
          // console.log(res.data)
          let code = res.data.code
          if (code === 200) {
            this.$message({
              type: 'success',
              message: '驳回成功',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {
            this.$message({
            type: 'info',
            message: '取消驳回'
          });  
       })
  },
  }
}
</script>
